package cn.com.checne.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.dao.NavigateInfoDao;
import cn.com.checne.model.domain.NavigateInfo;
import cn.com.checne.util.WebUtils;

@Controller
@RequestMapping(value="/sysSetUp")
public class SysSetUpFrontController {
	protected static final Logger logger = Logger.getLogger(SysSetUpFrontController.class);

	@Resource
	private NavigateInfoDao naInfoDao;
	
	//系统配置管理--首页
	@RequestMapping(value="/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/frame/frame_sysSetUp";
	}
	
	//系统配置管理--左侧导航
	@RequestMapping(value="/left.do")
	public String left(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/sysSetUp/left";
	}
	
	//系统配置管理--列举所有的导航内容
	@RequestMapping(value="/navigate-list.do")
	public ModelAndView navigateList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = WebUtils.getRequestParameterAsInt(request, "languageId", 0);
		List<NavigateInfo> navigateList = naInfoDao.getAll(languageId);
		modelMap.put("navigateList", navigateList);
		
		mav.setViewName("super/sysSetUp/navigate-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统配置管理--新增、修改页面
	@RequestMapping(value="/navigate-show.do")
	public ModelAndView navigateShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int naId = WebUtils.getRequestParameterAsInt(request, "naId", 0);
		NavigateInfo na = new NavigateInfo();
		String opt = "create" ;
		if(naId > 0){
			na = naInfoDao.getNavigate(naId);
			opt = "update" ;
		}
		modelMap.put("na", na);
		modelMap.put("opt", opt);
		mav.setViewName("super/sysSetUp/navigate-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统配置管理--新增
	@RequestMapping(value="/navigate-create.do")
	public ModelAndView navigateCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		NavigateInfo na = new NavigateInfo();
		na.setName(WebUtils.getRequestParameterAsString(request, "name"));
		na.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		na.setMark(WebUtils.getRequestParameterAsString(request, "mark"));
		na.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId", 1));
		na.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(naInfoDao.create(na)){
			message = "导航创建成功!" ;
		}else{
			message = "导航创建失败!" ;
		}
		modelMap.put("message", message);
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统配置管理--修改
	@RequestMapping(value="/navigate-update.do")
	public ModelAndView navigateUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		NavigateInfo na = new NavigateInfo();
		int naId = WebUtils.getRequestParameterAsInt(request, "naId", 0);
		na = naInfoDao.getNavigate(naId);
		na.setName(WebUtils.getRequestParameterAsString(request, "name"));
		na.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		na.setMark(WebUtils.getRequestParameterAsString(request, "mark"));
		na.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId", 1));
		na.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(naInfoDao.update(na)){
			message = "导航修改成功!" ;
		}else{
			message = "导航修改失败!" ;
		}
		modelMap.put("message", message);
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统配置管理--删除
	@RequestMapping(value="/navigate-del.do")
	public ModelAndView navigateDel(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		int naId = WebUtils.getRequestParameterAsInt(request, "naId", 0);
		if(naInfoDao.delete(naId)){
			message = "导航删除成功!" ;
		}else{
			message = "导航删除失败!" ;
		}
		modelMap.put("message", message);
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
}